'use strict';

const Controller = require('./base');

class BookController extends Controller {
    async page() {
        const { ctx } = this
        const query = ctx.query
        ctx.validate({
            page: { type: 'string', require: true },
            limit: { type: 'string', require: true }
        }, query)
        this.success(await ctx.service.book.page(query))
    }
    async create() {
        const { ctx } = this
        const body = ctx.request.body
        console.log(body);
        ctx.validate({
            book_num: { type: 'string', require: true },
            book_name: { type: 'string', require: true },
            writer: { type: 'string', require: true },
            publisher:{ type: 'string', require: true },
            introduction: { type: 'string', require: true },
            price: { type: 'integer', require: true },
            amount:{ type: 'integer', require: true },
            site: { type: 'string', require: true },
        }, body)
        const res = await ctx.service.book.create(body)
        if (res) {
            this.success('图书入库成功!')
        } else {
            this.error('图书入库失败!')
        }
    }
    async update() {
        const { ctx } = this
        const body = ctx.request.body
        ctx.validate({
            book_num: { type: 'string', require: true },
        }, body);
        const res = await ctx.service.book.update(body)
        if (res.success) {
            this.success('修改成功!')
        } else {
            this.error('修改失败!')
        }
    }
    async delete() {
        const { ctx } = this
        const body = ctx.request.body
        ctx.validate({
            book_num: { type: 'string', require: true },
        }, body);
        const res = await ctx.service.book.delete(body)
        if (res.success) {
            this.success('删除成功!')
        } else {
            this.error('删除失败!')
        }
    }
    async getUserInfoByBook() {
        const { ctx } = this
        const body = ctx.request.body
        this.success(await ctx.service.book.getUserInfoByBook(body))
    }
}

module.exports = BookController;
